]> git.r.bdr.sh - rbdr/super-polarity/blame - Super Polarity/Actors/MainShip.cs
Protoshow sprint.
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
CommitLineData
2af83e98
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
38c7d3f9 5using System.Threading;
2af83e98
BB
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Content;
8using Microsoft.Xna.Framework.Graphics;
b587e9d8 9using Microsoft.Xna.Framework.Audio;
2af83e98
BB
10
11namespace SuperPolarity
12{
13 class MainShip : Ship
14 {
38c7d3f9
BB
15
16 public static Color BlueColor;
17 public static Color RedColor;
18
2af83e98
BB
19 ParticleEngine particleEngine;
20
38c7d3f9
BB
21 protected bool Shooting;
22 protected int ShotCooldown;
23
74c15570
BB
24 protected float CurrentImmortalTime;
25 protected float MaxImmortalTime;
26 protected bool Flashing;
27
b587e9d8
BB
28 protected SoundEffect PolarityChange;
29 protected SoundEffect ShootSound;
30 protected SoundEffect Hit;
31
74c15570 32 public MainShip(SuperPolarity newGame) : base(newGame) {}
2af83e98 33
38c7d3f9
BB
34 ~MainShip()
35 {
36 particleEngine = null;
37 }
38
2af83e98
BB
39 public override void Initialize(Texture2D texture, Vector2 position)
40 {
41 base.Initialize(texture, position);
42
b587e9d8
BB
43 MainShip.BlueColor = new Color(0, 184, 229);
44 MainShip.RedColor = new Color(201, 0, 68);
45
46 PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
47 ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
48 Hit = game.Content.Load<SoundEffect>("Sound\\hit");
38c7d3f9 49
2af83e98
BB
50 InitParticleEngine();
51 SetPolarity(Polarity.Positive);
52
b587e9d8
BB
53 ActVelocity = 2.5f;
54
38c7d3f9 55 ShotCooldown = 50;
74c15570
BB
56 MaxImmortalTime = 1500;
57
58 BoxDimensions.X = 2;
59 BoxDimensions.Y = 2;
60 BoxDimensions.W = 2;
61 BoxDimensions.Z = 2;
62 InitBox();
2af83e98
BB
63
64 BindInput();
65 }
66
67 void InitParticleEngine()
68 {
38c7d3f9 69 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
2af83e98
BB
70 }
71
72 void BindInput()
73 {
74 InputController.Bind("moveX", HandleHorizontalMovement);
75 InputController.Bind("moveY", HandleVerticalMovement);
76 InputController.Bind("changePolarity", HandleChangePolarity);
38c7d3f9
BB
77 InputController.Bind("shoot", HandleShot);
78 }
79
80 protected void HandleShot(float value)
81 {
b587e9d8
BB
82
83 Shooting = true;
84 Timer t = new Timer(new TimerCallback(UnlockShot));
85 t.Change(ShotCooldown, Timeout.Infinite);
86
87 if (Children.Count > 10)
88 {
89 return;
90 }
91
74c15570
BB
92 var bullet = ActorFactory.CreateBullet(Position, Angle);
93
94 Children.Add(bullet);
95 bullet.Parent = this;
96
b587e9d8 97 ShootSound.Play();
38c7d3f9
BB
98 }
99
100 protected void UnlockShot(object state)
101 {
102 InputController.Unlock("shoot");
b587e9d8 103 Shooting = false;
2af83e98
BB
104 }
105
106 protected void HandleChangePolarity(float value)
107 {
108 SwitchPolarity();
109 }
110
111 public void HandleHorizontalMovement(float value)
112 {
b587e9d8 113 if (value >= -0.5 && value <= 0.5)
2af83e98 114 {
b587e9d8 115 value = 0;
2af83e98
BB
116 }
117
b587e9d8 118 Velocity.X = value * MaxVelocity;
2af83e98
BB
119 }
120
121 public void HandleVerticalMovement(float value)
122 {
b587e9d8
BB
123 if (value >= -0.5 && value <= 0.5)
124 {
125 value = 0;
126 }
127
128 Velocity.Y = value * MaxVelocity;
2af83e98
BB
129 }
130
131 public override void SwitchPolarity()
132 {
133 base.SwitchPolarity();
134 SwitchParticleEngine(CurrentPolarity);
b587e9d8 135 PolarityChange.Play();
74c15570 136 game.Player.ResetMultiplier();
2af83e98
BB
137 }
138
139 public override void SetPolarity(Polarity newPolarity)
140 {
141 base.SetPolarity(newPolarity);
142 SwitchParticleEngine(newPolarity);
143 }
144
145 protected void SwitchParticleEngine(Polarity polarity)
146 {
147 if (polarity == Polarity.Positive)
148 {
38c7d3f9 149 particleEngine.Color = MainShip.RedColor;
2af83e98
BB
150 }
151 else if (polarity == Polarity.Negative)
152 {
38c7d3f9 153 particleEngine.Color = MainShip.BlueColor;
2af83e98
BB
154 }
155 else
156 {
157 particleEngine.Color = Color.Gray;
158 }
159 }
160
161 public override void Update(GameTime gameTime)
162 {
163 base.Update(gameTime);
164 particleEngine.EmitterLocation = Position;
165 particleEngine.Update();
166 ConstrainToEdges();
74c15570 167 UpdateImmortality(gameTime);
38c7d3f9
BB
168 }
169
74c15570
BB
170 public void UpdateImmortality(GameTime gameTime)
171 {
172 if (Immortal)
173 {
174 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
175
176 if (Flashing)
177 {
178 Color = new Color(255, 255, 255, 128);
179 }
180 else
181 {
182 Color = Color.White;
183 }
184
185 Flashing = !Flashing;
186
187 if (CurrentImmortalTime > MaxImmortalTime)
188 {
189 Immortal = false;
190 Color = Color.White;
191 }
192 }
193 }
194
38c7d3f9
BB
195 public override void Move(GameTime gameTime)
196 {
b587e9d8
BB
197 var VelocityLimit = MaxVelocity;
198 var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);
38c7d3f9
BB
199
200 if (Shooting)
201 {
b587e9d8
BB
202 VelocityLimit = ActVelocity;
203 }
38c7d3f9 204
b587e9d8
BB
205 if (SavedVelocity.X > VelocityLimit)
206 {
207 SavedVelocity.X = VelocityLimit;
208 }
38c7d3f9 209
b587e9d8
BB
210 if (SavedVelocity.X < -VelocityLimit)
211 {
212 SavedVelocity.X = -VelocityLimit;
213 }
38c7d3f9 214
b587e9d8
BB
215 if (SavedVelocity.Y > VelocityLimit)
216 {
217 SavedVelocity.Y = VelocityLimit;
218 }
219
220 if (SavedVelocity.Y < -VelocityLimit)
221 {
222 SavedVelocity.Y = -VelocityLimit;
38c7d3f9 223 }
b587e9d8
BB
224
225 Position.X = Position.X + SavedVelocity.X;
226 Position.Y = Position.Y + SavedVelocity.Y;
2af83e98
BB
227 }
228
229 public override void Magnetize(Ship ship, float distance, float angle)
230 {
231 }
232
233 protected void ConstrainToEdges()
234 {
235 if (Position.X < 0)
236 {
237 Position.X = 0;
238
239 if (Velocity.X < 0)
240 {
241 Velocity.X = 0;
242 }
243 }
244 if (Position.X > game.GraphicsDevice.Viewport.Width)
245 {
246 Position.X = game.GraphicsDevice.Viewport.Width;
247
248 if (Velocity.X > 0)
249 {
250 Velocity.X = 0;
251 }
252 }
253 if (Position.Y < 0)
254 {
255 Position.Y = 0;
256
257 if (Velocity.Y < 0)
258 {
259 Velocity.Y = 0;
260 }
261 }
262 if (Position.Y > game.GraphicsDevice.Viewport.Height)
263 {
264 Position.Y = game.GraphicsDevice.Viewport.Height;
265
266 if (Velocity.Y < 0)
267 {
268 Velocity.Y = 0;
269 }
270 }
271 }
272
273 public override void Draw(SpriteBatch spriteBatch)
274 {
275 particleEngine.Draw(spriteBatch);
276 base.Draw(spriteBatch);
277 }
74c15570
BB
278
279 public override void Collide(Actor other, Rectangle collision)
280 {
281 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
282 !Immortal)
283 {
284 Die();
285 }
286 }
287
288 protected override void Die()
289 {
290 game.Player.Lives = game.Player.Lives - 1;
291 game.Player.ResetMultiplier();
292 if (game.Player.Lives < 0)
293 {
294 Dying = true;
b587e9d8 295 game.GameOver();
74c15570
BB
296 }
297 else {
b587e9d8 298 Hit.Play();
74c15570
BB
299 Immortal = true;
300 CurrentImmortalTime = 0;
301 }
302 }
b587e9d8
BB
303
304 public override void CleanUp()
305 {
306 base.CleanUp();
307 particleEngine = null;
308 InputController.Unbind("moveX", HandleHorizontalMovement);
309 InputController.Unbind("moveY", HandleVerticalMovement);
310 InputController.Unbind("changePolarity", HandleChangePolarity);
311 InputController.Unbind("shoot", HandleShot);
312 }
2af83e98
BB
313 }
314}